package com.lwb.ztx.news.model;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lwb.ztx.bean.NewsAndBannerBean;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.bean.NewsDetailBean;
import com.lwb.ztx.bean.NewsSlideBean;
import com.lwb.ztx.bean.NewsTopicBean;
import com.lwb.ztx.utils.JsonUtils;
import com.lwb.ztx.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NewsJsonUtils {

    private final static String TAG = "NewsJsonUtils";
    private final static int TYPE_TOPIC = 10;
    private final static int TYPE_NORMAL = 11;


    public static List<NewsSlideBean> readJsonNewsSlideBeans(String res) {
        List<NewsSlideBean> list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(res);
            JSONObject body = jsonObject.getJSONObject("body");
            JSONArray slides = body.getJSONArray("slides");
            for (int i=0;i<slides.length();i++) {
                NewsSlideBean bean = new NewsSlideBean();
                bean.setDescription(slides.getJSONObject(i).optString("description"));
                bean.setImage(slides.getJSONObject(i).optString("image"));
                bean.setTitle(slides.getJSONObject(i).optString("title"));
                list.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static NewsDetailBean readJsonNewsDetailBean(String res) {
        NewsDetailBean bean  = new NewsDetailBean();
        try {
            JSONObject json = new JSONObject(res);
            JSONObject meta = json.optJSONObject("meta");
            bean.setId(meta.optString("id",""));
            bean.setType(meta.optString("type",""));
            JSONObject body = json.optJSONObject("body");
            bean.setTitle(body.optString("title"));
            bean.setSource(body.optString("source"));
            bean.setText(body.optString("text"));
            bean.setThumbnail(body.optString("thumbnail"));
            bean.setUpdateTime(body.optString("updateTime"));
            JSONArray images = body.optJSONArray("img");
            if(null != images) {
                String img[] = new String[images.length()];
                for (int i = 0; i < images.length(); i++) {
                    JSONObject image = images.getJSONObject(i);
                    img[i] = image.optString("url", "");
                }
                bean.setImages(img);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return bean;
    }

    /**
     * 解析专题新闻
     * @param res
     * @return
     */
    public static List<NewsTopicBean>  readJsonNewsTopicBeans(String res) {
        List<NewsTopicBean> beans = new ArrayList<>();
        try {
            JSONObject jsonArray = new JSONObject(res);
            JSONObject body = jsonArray.optJSONObject("body");
            JSONArray subjects = body.optJSONArray("subjects");
            for (int i=0;i<subjects.length();i++)  {
                NewsTopicBean bean = new NewsTopicBean();
                JSONObject subject = subjects.getJSONObject(i);
                bean.setView(subject.optString("view"));
                JSONObject content = subject.optJSONObject("content");
                if(content != null) {
                    bean.setBannerUrl(content.optString("customBanner", ""));
                    bean.setIntro(content.optString("intro", ""));
                }
                if(bean.getView().equals("slider")) {
                    List<NewsBean> sliders = new ArrayList<>();
                    JSONArray slidersJson = subject.getJSONArray("podItems");
                    for(int j=0;j<slidersJson.length();j++) {
                        JSONObject item = slidersJson.getJSONObject(j);
                        sliders.add(getNewsBean(item,TYPE_TOPIC));
                    }
                    bean.setSliders(sliders);
                }

                if(bean.getView().equals("list")) {
                    List<NewsBean> podItem = new ArrayList<>();
                    JSONArray podItemJson = subject.getJSONArray("podItems");
                    String title = subject.optString("title","");
//                    String navTitle = subject.optString("navTitle","");
                    for(int j=0;j<podItemJson.length();j++) {
                        JSONObject item = podItemJson.getJSONObject(j);
                        podItem.add(getNewsBean(item,TYPE_TOPIC));
                    }
                    bean.setPodItems(podItem);
                    addTopicItem(beans,bean,title.equals("") ? subject.optString("navTitle",""):title);
                    continue;
                }
                beans.add(bean);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return beans;
    }

    private static void addTopicItem(List<NewsTopicBean> beans, NewsTopicBean podItem,String navTitle) {
        LogUtils.d("TTTTT" + podItem.getPodItems().toString());
        if(podItem.getPodItems().size() == 0) {
            return;
        }
        NewsTopicBean bean = new NewsTopicBean();
        bean.setView("podName");
        bean.setTitle(navTitle.equals("") ? "焦点新闻":navTitle);
        beans.add(bean);
        for(int i=0;i<podItem.getPodItems().size();i++) {
            NewsTopicBean a = new NewsTopicBean();
            a.setView("newsBean");
            a.setNewsBean(podItem.getPodItems().get(i));
            beans.add(a);
        }
    }

    /**
     * 将获取到的json转换为新闻列表对象
     * @param res
     * @return
     */
    public static NewsAndBannerBean readJsonNewsBeans(String res) {
        List<NewsBean> beans = new ArrayList<NewsBean>();
        List<NewsBean> bannerBeans = new ArrayList<NewsBean>();
        try {
            if(res.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(res);
                if (jsonArray.length() > 0) {
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    JSONArray items = jsonObject.optJSONArray("item");
                    if (null != items) {
                        for (int i = 0; i < items.length(); i++) {
                            JSONObject item = items.getJSONObject(i);
                            beans.add(getNewsBean(item,TYPE_NORMAL));
                        }
                    }
                    if(jsonArray.length() > 1) {
                        JSONObject banner = jsonArray.getJSONObject(1);
                        JSONArray banners = banner.optJSONArray("item");
                        if (null != banners) {
                            for (int i = 0; i < banners.length(); i++) {
                                JSONObject item = banners.getJSONObject(i);
                                bannerBeans.add(getNewsBean(item,TYPE_NORMAL));
                            }
                        }
                    }
                }
            } else if(res.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(res);
                JSONArray items = jsonObject.optJSONArray("item");
                if (null != items) {
                    for (int i = 0; i < items.length(); i++) {
                        JSONObject item = items.getJSONObject(i);
                        beans.add(getNewsBean(item,TYPE_NORMAL));
                    }
                }
                JSONArray bodys = jsonObject.optJSONArray("body");
                if(null != bodys) {
                    for (int i = 0; i < bodys.length(); i++) {
                        JSONObject item = bodys.getJSONObject(i);
                        beans.add(getNewsBean(item,TYPE_NORMAL));
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.e("readJsonNewsBeans error" , e);
        }
        NewsAndBannerBean newsAndBannerBean = new NewsAndBannerBean();
        newsAndBannerBean.setBannerBeans(bannerBeans);
        newsAndBannerBean.setBeans(beans);
        return newsAndBannerBean;
    }

    public static NewsBean getNewsBean(JSONObject item, int newType) {
        LogUtils.d("TTTDD" + item.toString());
        NewsBean bean = new NewsBean();
        bean.setComments(item.optString("comments",""));
        bean.setCommentsAll(item.optString("commentsall",""));
        bean.setCommentsUrl(item.optString("commentsUrl",""));
        bean.setDocumentId(item.optString("documentId",""));
        bean.setId(item.optString("id",""));
        if(newType != TYPE_TOPIC) {
            JSONObject link = item.optJSONObject("link");
            if (null != link) {
                bean.setLinkUrl(link.optString("url", ""));
            }
        }
        if(newType == TYPE_TOPIC) {
            bean.setCommentsAll(item.optString("commentCount",""));

            JSONArray links = item.optJSONArray("links");
            if(null != links)
            for (int i=0;i<links.length();i++){
                try {
                    if(i == 0) {
                        JSONObject linkItem = links.getJSONObject(i);
                        bean.setType(linkItem.optString("type"));
                        bean.setLinkUrl(linkItem.optString("url"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        bean.setSource(item.optString("source",""));
        bean.setThumbnail(item.optString("thumbnail",""));
        bean.setTitle(item.optString("title",""));
        bean.setUpdateTime(item.optString("updateTime",""));
        if(newType != TYPE_TOPIC){
            bean.setType(item.optString("type",""));
        }
        if(bean.getType().equals("slide")) {
            JSONObject style = item.optJSONObject("style");
            if (null != style) {
                JSONArray images = style.optJSONArray("images");
                if(images.length() > 0) {
                    ArrayList<String> imageList = new ArrayList<>();
                    for (int m=0;m<images.length();m++) {
                        try {
                            imageList.add(images.getString(m));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    bean.setImages(imageList);
                }
            }
        }
        return bean;
    }

}
